/**
 * Copyright 2006 Bosco Curtu
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * 
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 * either express or implied. 
 * 
 * See the License for the specific language governing permissions and limitations 
 * under the License.
*/
package com.bcurtu.utils;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.bcurtu.db.AccountDao;
import com.bcurtu.james.JamesManager;


public class TempAlias {

	public static final String CONTEXT_CONFIG_JAMES = "bin/applicationContext-james.xml";
	public static final String CONTEXT_DB_CONFIG_LOCATION = "bin/applicationContext-db-test.xml";

	private JamesManager james;
	private AccountDao accManager;
	private Logger log =Logger.getLogger(this.getClass());
	 
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		TempAlias mf=new TempAlias();
		
		if (args.length==0) {
			System.out.println("Usage TempAlias [shutdown|clean]");
			System.exit(0);
		}
		String cmd=args[0];
		
		mf.setup();
		
		if (cmd.equals("shutdown"))
			mf.shutdown();
		else if (cmd.equals("clean"))
			mf.clean();
	}
	
	private void setup(){
		ApplicationContext context = new FileSystemXmlApplicationContext(new String[]{
				CONTEXT_CONFIG_JAMES,
				CONTEXT_DB_CONFIG_LOCATION
		});
		james = (JamesManager) context.getBean("JamesManager");
		accManager = (AccountDao) context.getBean("AccountManager");
	}
	
	private void shutdown() throws Exception{
		log.debug("Shutdown JAMES");
		james.shutdownJames();
	}
	
	private void clean() throws Exception{
		log.debug("Clean JAMES");
		james.cleanAllAccounts();
	}

}
